/**
 * 合同管理
 * @author 牧羊人
 * @since 2021/12/21
 */
layui.use(['dropdown','function','form'], function () {
    //声明变量
    var func = layui.function
        , form = layui.form
        , dropdown = layui.dropdown
        , $ = layui.$;

    if (A == 'index') {
        //【TABLE列数组】
        var cols = [
            {type: 'checkbox', fixed: 'left'}
            , {field: 'company_name', width: 250, title: '用户', align: 'center'}
            , {field: 'signing_date', width: 150, title: '签订日期', align: 'center'}
            , {field: 'contract_no', width: 200, title: '合同编号', align: 'center'}
            , {field: 'amount', width: 100, title: '合同金额', align: 'center'}
            , {field: 'type', width: 100, title: '收费情况', align: 'center', templet: function (d) {             
                    if (d.type=='1') {
                        return "部分款项";
                    }else if(d.type=='0'){
                        return "全款";
                    }else{
                        return '';
                    }
                }
            }
            // , {field: 'create_user_name', width: 100, title: '创建人', align: 'center'}
            , {field: 'create_time', width: 180, title: '创建时间', align: 'center', sort: true}
            , {fixed: 'right', width: 250, title: '功能操作', align: 'center', toolbar: '#toolBar'}
        ];
        
        //【渲染TABLE】
        func.tableIns(cols, "tableList",null,location.href);

        //【设置弹框】
        func.setWin("合同", 850, 600);
    }

    $("#selectInput").on('keyup',function(){
        searchUp()
    })
    
    var inst = dropdown.render({});
    
    window.search = function () 
    {   
        var value = $("#selectInput").val()??'',pid=$("#selectPid").attr("attr_pid"),select_data='';
        $("#company_id").val('')
        $.ajax({
           type:'GET',
           url:'/ofcompany/getList',
           data:{"name":value},
           success:function(res){
                // 返回成功的数据
                var da = JSON.parse(res),str = '';
                if(da.code==0){
                    inst.reload({ 
                        elem: '#selectInput'
                        ,show: true //重载即显示
                        ,data: da.data
                        ,click: function(obj){
                          this.elem.val(obj.title);
                          $("#company_id").val(obj.id)
                        }
                    });
                    
                }
           }
        });
        
    }

    $(".multiple a span").on("click",function(e){
        console.log(e.currentTarget.getAttribute('lay-value'))
        get_data(e.currentTarget.getAttribute('lay-value'),e.currentTarget.innerHTML)
    })


    // window.get_data = function (filename)
    // {            
    //     $.ajax({
    //         type:'GET',
    //         url:'download',
    //         data:{"filename":filename},
    //         success:function(res){
    //             data = JSON.parse(res)
    //             if(data.code == 0){
    //                 window.open(cUrl+'/../uploads'+data.data['filename'])
    //             }else{
    //                 layer.msg(data.msg)
    //             }
                
    //         }
    //     });
    // }

    window.get_data = function (url,name)
    {            
        // $.ajax({
        //     type:'GET',
        //     url:'rename',
        //     data:{"filename":filename,'id':id},
        //     success:function(res){
        //         data = JSON.parse(res)
        //         if(data.code == 0){
        //             window.open('/ofcontract/rename?filename='+data.data['filename']+'&newfilename=ceshi');
        //         }else{
        //             layer.msg(data.msg)
        //         }
                
        //     }
        // });
       window.location.href = '/ofcontract/rename?url='+url+"&name="+name;
    }

    var flag;  //全局变量用于标识是否延时执行keyup事件

    function searchUp()
    {
        clearTimeout(flag);
        flag = setTimeout(function(){ 
            search();
        }, 300);
    }

    //监听 select 根据值显示隐藏
    form.on('select(type)', function(data){
        //等于0 全部隐藏 否则对应显示
        console.log(data.value)

        select_change(data.value);
    })

    select_change($("#type").val());

    function select_change($value){
        console.log($value);
        $('#type_desc').find('div').each(function(i,o){
            $(o).css('display','none');
            if(i==$value-1){
                $(o).css('display','block');
            }
        });
    }

    //监听 standard 根据值显示隐藏
    form.on('select(standard)', function(data){
        //等于0 全部隐藏 否则对应显示
        console.log(data.value)

        standard_change(data.value);
    })

    standard_change($("#standard").val());

    function standard_change($value){
        $('#standard_desc').find('div').each(function(i,o){
            $(o).css('display','none');
            if(i==$value-1){
                $(o).css('display','block');
            }
        });
    }
    $("#batch_import").on("click",function(){
        var url = cUrl + "/../ofcontract/batch_import";
        func.showWin("批量导入", url,500,500);
    })


    $('#download').on("click",function(e){
            // 选择数据
        
        layer.confirm('即将导出',
           {icon: 3, title: '批量导出'}, 
           function (index) {
            layer.close(index);
            isDeploy=true;
             get_data2()
        });
    })




    window.get_data2 = function (ids)
    {   
        var company_id = $("[name='company_id']").val();
        var contract_no = $("[name='contract_no']").val();
        window.location.href = '/ofcontract/excel?company_id='+company_id+"&contract_no="+contract_no;
    }

});
